﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Application;
using AutoMapper;
using J.M.Inventory.Application.IInventoryServices;
using J.M.Inventory.Domain.DTO;
using J.M.Inventory.Domain.Entity;
using J.M.Inventory.Infrastruecure.IInventoryRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace J.M.Inventory.Application.InventoryServices
{
    /// <summary>
    /// 盘点库存表服务层实现
    /// </summary>
    public class Examine_warehouseServices : BaseServices<examine_warehouse, string>, IExamine_warehouseServices
    {
        //仓库表
        private readonly IWarehouseRepository warehouseRepository;
        private readonly IExamine_warehouseRepository examine_warehouseRepository;
        private readonly IMapper mapper;

        public Examine_warehouseServices(IExamine_warehouseRepository examine_warehouseRepository, IMapper mapper, IWarehouseRepository warehouseRepository) : base(examine_warehouseRepository)
        {
            this.mapper = mapper;
            this.examine_warehouseRepository = examine_warehouseRepository;
            this.warehouseRepository = warehouseRepository;
        }
        /// <summary>
        /// 盘点库存表显示
        /// </summary>
        /// <param name="code"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public PageETO<examine_warehouseDTO> GetExamine_warehouseShow1(string? code, int pageIndex, int pageSize)
        {
            PageETO<examine_warehouseDTO> pageETO = new PageETO<examine_warehouseDTO>();
            Expression<Func<examine_warehouse, bool>> exception = i => true;
            if (!string.IsNullOrEmpty(code))
            {
                exception = i => i.checkinventory_code.Contains(code);
            }
            //var list=await examine_warehouseRepository.GetShowQueryableAsync(pageSize,pageIndex, exception);
            //pageETO.TotalCount = list.TotalCount;
            //pageETO.PageCount=list.PageCount;
            //var res=mapper.Map<List<examine_warehouseDTO>>(list.Data).OrderByDescending(x=>x.checkinventory_datetime).ToList();
            //pageETO.Data = res;
            //return pageETO;
            List<examine_warehouse>? examine_warehouse = examine_warehouseRepository.GetShowQueryable(exception).ToList();
             List<warehouse>? warehouse = warehouseRepository.GetShowQueryable().ToList();

            var list = (from a in examine_warehouse
                        join b in warehouse
                        on a.checkinventory_warehouseid equals b.warehousecode

                        select new examine_warehouseDTO
                        {
                            checkinventory_accessory = a.checkinventory_accessory,
                            checkinventory_code = a.checkinventory_code,
                            checkinventory_datetime = a.checkinventory_datetime,
                            checkinventory_id = a.checkinventory_id,
                            checkinventory_personnel = a.checkinventory_personnel,
                            checkinventory_remarks = a.checkinventory_remarks,
                            checkinventory_warehouseid = a.checkinventory_warehouseid,
                            checkinventory_subject = a.checkinventory_subject,
                            warehousename = b.warehousename,
                            warehousecode = b.warehousecode,
                            checkinventory_state = a.checkinventory_state,
                        });
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount / (double)pageSize);
            pageETO.Data = list.OrderByDescending(x => x.checkinventory_datetime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
    }
}
